<!-- 预约情况列表 -->
<template>
  <div class="ky-container">
    <el-card shadow="hover" class="ky-card ky-card-hdbg">
      <el-form :inline="true" class="ky-form-inline ky-form-inline-sm" label-width="82px">
        <el-form-item label="医院名称:" prop="hospitalCode">
          <el-select
            size="small"
            class="param-type_input"
            v-model="formData.hospitalCode"
            filterable
            placeholder="请选择"
            @change="handleChange('hospital')">
            <el-option
              v-for="(item,index) in mx_hospitalList"
              :key="index"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="影像区域:">
          <el-select size="small" class="param-type_input" v-model="formData.depArea" multiple placeholder="请选择" clearable collapse-tags @change="handleChange('depArea')">
            <el-option
              v-for="(item,index) in depAreaOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查部门:">
          <el-select size="small" class="param-type_input" v-model="formData.dep" multiple placeholder="请选择" clearable collapse-tags @change="handleChange('dep')">
            <el-option
              v-for="(item,index) in scheduledDepOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查设备:">
          <el-select size="small" class="param-type_input" v-model="formData.modality" multiple placeholder="请选择" clearable collapse-tags @change="handleChange('modality')">
            <el-option
              v-for="(item,index) in modalityOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查机房:">
          <el-select size="small" class="param-type_input" v-model="formData.checkRoom" multiple placeholder="请选择" clearable collapse-tags>
            <el-option
              v-for="(item,index) in checkRoomOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预约日期:">
          <el-date-picker
            v-model="formData.appointmentDate"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button class="m-l-0" size="small" @click="resetParam" plain>重置</el-button>
          <el-button type="primary" class="m-l-0" size="small" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="hover" class="ky-card ky-card-hdbg ky-mt10">
      <div class="content-title">预约情况列表</div>
      <el-table
        ref="table"
        v-loading="loading"
        size="medium"
        :data="tableData"
        style="width: 100%"
        border
        class="ky-table"
      >
        <el-table-column align="left" prop="hospital" label="医院"></el-table-column>
        <el-table-column align="left" prop="videoDistribution" label="影像分布"></el-table-column>
        <el-table-column align="left" prop="inspectionDepartment" label="科室"></el-table-column>
        <el-table-column align="left" prop="checkRoom" label="检查部门"></el-table-column>
        <el-table-column align="left" prop="computerRoom" label="机房"></el-table-column>
        <el-table-column align="right" header-align="left" prop="total" label="总数"></el-table-column>
        <el-table-column align="right" header-align="left" prop="haveAppointment" label="已预约">
          <template slot-scope="scope">
            <span class="blue-text">{{scope.row.haveAppointment}}</span>
          </template>
        </el-table-column>
        <el-table-column align="right" header-align="left" prop="morning" label="上午"></el-table-column>
        <el-table-column align="right" header-align="left" prop="afternoon" label="下午"></el-table-column>
      </el-table>
      <paginationCont
        :total="totalCount"
        :page.sync="currentPage"
        :limit.sync="pageSize"
        @pagination="fetchData"
      />
    </el-card>
  </div>
</template>

<script>
import { getAppointmentRecordsInfoPages, getDepAreaList, getDepList, getModalityList } from '@/api/api.js'
import paginationCont from '@/components/Pagination/index'
import { operateTypeFlr } from '@/utils/validate'
export default {
  name: '',
  mixins: [],
  components: { paginationCont },
  filters: {
    operateTypeFlr
  },
  data() {
    return {
      formData: {
        hospitalCode: this.$store.state.user.hospitalCode,
        videoDistribution: [],
        inspectionDepartment: [],
        dep: [],
        depArea: [],
        modality: [],
        checkRoom: [],
        appointmentDate: []
      },
      depAreaOptions: [],
      scheduledDepOptions: [],
      modalityOptions: [],
      checkRoomOptions: [],
      videoDistributionOptions: [],
      inspectionDepartmentOptions: [],
      loading: false,
      tableData: [
        {
          hospital: '北京大学第一医院',
          videoDistribution: '本院',
          inspectionDepartment: '影像科',
          checkRoom: '超声检查',
          computerRoom: 'B超1',
          total: '2000',
          haveAppointment: '1000',
          morning: '500',
          afternoon: '500'
        }
      ],
      totalCount: 0,
      pageSize: 10,
      currentPage: 1,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  watch: {},
  computed: {},
  created() {
    this.$getHospitalList()
  },
  mounted() {
    this.formData.hospitalCode = this.$store.state.user.hospitalCode
    /* this.getOperateUserOptions()
    this.getOperateTypeOptions()
    this.getOperateRoomOptions()
    this.search() */
  },
  methods: {
    frtTime(row, column, cellValue, index) {
      // 处理后台时间
      let time = ''
      if (cellValue) {
        time = cellValue.slice(0, 10)
      }
      return time
    },
    resetParam() {
      // 重置
      this.formData = {
        hospitalCode: this.$store.state.user.hospitalCode,
        operateUser: [],
        operateType: [],
        timeRange: [],
        operateRoom: []
      }
      this.search()
    },
    search() {
      // 查询
      this.currentPage = 1
      this.fetchData()
    },
    fetchData() {
      this.loading = true
      let data = {
        hospitalCode: this.formData.hospitalCode,
        operateUser: this.formData.operateUser.join(','),
        operateType: this.formData.operateType.join(','),
        startTime: this.formData.timeRange && this.formData.timeRange.length > 0 ? this.formData.timeRange[0] : '',
        endTime: this.formData.timeRange && this.formData.timeRange.length > 0 ? this.formData.timeRange[1] : '',
        operateRoom: this.formData.operateRoom.join(','),
        pageSize: this.pageSize, // 页面大小
        pageNumber: this.currentPage // 页码
      }
      getAppointmentRecordsInfoPages(data).then(res => {
        this.loading = false
        if (res && res.code === 10000) {
          this.tableData = res.content.list
          this.totalCount = res.content.totalCount
        }
      }).catch(() => {
        this.loading = false
      })
    },
    getOperateUserOptions() {
      // 从后台获取操作人员列表
      this.operateUserOptions = []
    },
    getOperateTypeOptions() {
      // 从后台获取操作类型
      this.operateTypeOptions = []
    },
    getOperateRoomOptions() {
      // 从后台获取操作机房
      this.operateRoomOptions = []
    },
    getDepAreaOptions() {
      /* 影像区域列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode
      }
      getDepAreaList(params).then((res) => {
        if (res.code === 10000) {
          this.depAreaOptions = res.content.list
        }
      })
    },
    getScheduledDepOptions() {
      /* 检查部门列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode,
        depArea: this.formData.depArea.join(',')
      }
      getDepList(params).then((res) => {
        if (res.code === 10000) {
          this.scheduledDepOptions = res.content.list
        }
      })
    },
    getModalityOptions() {
      /* 检查设备列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode,
        depArea: this.formData.depArea.join(','),
        dep: this.formData.dep.join(',')
      }
      getModalityList(params).then((res) => {
        if (res.code === 10000) {
          this.modalityOptions = res.content.list
        }
      })
    },
    getCheckRoomOptions() {
      /* 检查机房列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode,
        depArea: this.formData.depArea.join(','),
        dep: this.formData.dep.join(','),
        modality: this.formData.modality.join(',')
      }
      getDepList(params).then((res) => {
        if (res.code === 10000) {
          this.checkRoomOptions = res.content.list
        }
      })
    },
    handleChange(val) {
      switch (val) {
        case 'hospital':
          this.getDepAreaOptions()
          this.getScheduledDepOptions()
          this.getModalityOptions()
          this.getCheckRoomOptions()
          this.formData.depArea = []
          this.formData.dep = []
          this.formData.modality = []
          this.formData.checkRoom = []
          break
        case 'depArea':
          this.getScheduledDepOptions()
          this.getModalityOptions()
          this.getCheckRoomOptions()
          this.formData.dep = []
          this.formData.modality = []
          this.formData.checkRoom = []
          break
        case 'dep':
          this.getModalityOptions()
          this.getCheckRoomOptions()
          this.formData.modality = []
          this.formData.checkRoom = []
          break
        case 'modality':
          this.getCheckRoomOptions()
          this.formData.checkRoom = []
          break
        default:
          break
      }
    }
  },
  beforeDestroy() {},
  destroyed() {},
  beforeUpdate() {},
  updated() {}
}
</script>
<style lang='scss' scoped>
.ky-container{
    .content-title{
        margin-left:4px;
        margin-bottom: 16px;
        font-size: 16px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 600;
        color: rgba(0, 0, 0, 0.85);
    }
    .blue-text{
        color:#1890FF
    }
    .click-button{
        width: 120px;
    }
}
.ky-form-inline-sm .el-form-item .el-date-editor--daterange.el-input__inner{
    width: 468px;
}
.ky-form-inline-sm .el-form-item /deep/ .el-input__inner{
    width: 180px;
}
.ky-form-inline-sm .el-form-item{
    margin-right: 22px;
}
</style>
